//
// Created by sht on 2025/7/10.
//

class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        if(nums.size() == 1 && nums[0] == x) return 1;
        int sum = 0;
        for(int i = 0 ; i < nums.size(); ++i)
        {
            sum += nums[i];
        }
        sum -= x;
        int len = -1;
        int l = 0,r = 0;
        int cur_sum = nums[0];
        while(l < nums.size() && r < nums.size())
        {
            if(cur_sum == sum)
            {
                len = r-l+1 > len ? r-l+1 : len;
                r++;
                if(r < nums.size()) cur_sum += nums[r];

            }
            else if(cur_sum < sum)
            {
                r++;
                if(r < nums.size()) cur_sum += nums[r];
            }
            else
            {
                l++;
                cur_sum -= nums[l-1];
            }
        }
        return len == -1 ? -1 : nums.size() - len;
    }
};